package tn.softcom.dfbuilder;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.IconStyle;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.Style;

/**
 * Servlet implementation class Map
 */
@WebServlet("/map")
public class Map extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Map() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	
		Style style = new Style();
		IconStyle icon = new IconStyle();
		icon.setColor("02ff02");
		style.setIconStyle(icon);
		style.setId("SourceStyle");

		final Kml kml = new Kml();
		Document document = kml.createAndSetDocument().withName("MyMarkers");
		 document
		 .createAndAddStyle().withId("style1")
		 .createAndSetIconStyle().withScale(1.0)
		 .createAndSetIcon().withHref("http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png");
		document.createAndAddPlacemark()
		   .withName("BAR4010F23").withOpen(Boolean.TRUE).withStyleUrl("#style1")
				.createAndSetPoint().addToCoordinates(10.2458, 36.763);
		document.createAndAddPlacemark()
		   .withName("BAR4011F01").withOpen(Boolean.TRUE).withDescription("aaaaaaaaaaaa bbbbbbb ").withAddress("sssssssssssssssssss")
				.createAndSetPoint().addToCoordinates(10.3109, 36.7425);
		//marshals to console
		kml.marshal();
		//marshals into file
		File f = new File("BAR4010F23.kml");
		kml.marshal(f);
		if (f.exists()) {
			int filesize = (int) f.length();
			byte[] buff = new byte[filesize];

			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition",
					"attachment; filename=" + f.getName());
			FileInputStream fin = new FileInputStream(f);
			BufferedInputStream bis = new BufferedInputStream(fin);
			ServletOutputStream fout = response.getOutputStream();
			BufferedOutputStream bos = new BufferedOutputStream(fout);
			int bytesRead;
			while ((bytesRead = bis.read(buff)) != -1) {
				bos.write(buff, 0, bytesRead);
			}
			bos.flush();

			fin.close();
			fout.close();
			bis.close();
			bos.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

}
